package character;

import config.Config;
import managers.InputManager;
import sprite.Animator;
import sprite.Transform;

import java.awt.*;
import java.awt.event.KeyEvent;

public class Player extends ActorObject{
    private Animator _animator;
    private InputManager _input = InputManager.getInstance();
    private float _xspeed = 3.0f;
    private float _yspeed = 1.5f;

    private boolean _keyPressed = false;
    private double _flipCord = 1;

    private int _topLocked = 0;
    private int _bottomLocked = 0;
    private int _leftLocked = 0;
    private int _rightLocked = 0;

    private boolean _attkKeyUp = true;

    private boolean _attack = false;
    private boolean _rdyToAttack = true;

    private final int _hitBoxDelay = 100;
    private final int _hitDelay = 320;
    private int _hitDelayTimer = _hitDelay;

    public Player(Animator animator, int hurtx, int hurty, int hurtscale_x, int hurtscale_y
            , int hitoffset_x1, int hitoffset_y1, int hitoffset_x2, int hitoffset_y2){
        super(animator,5,25,hurtx,hurty,hurtscale_x,hurtscale_y
                ,hitoffset_x1,hitoffset_y1,hitoffset_x2,hitoffset_y2);
//        _animator = new Animator(animName);
        _animator = animator;
        _animator.setAnimation("idle");
    }

    public void setLockedBounds(int top,int bottom,int left, int right){
        _topLocked = top;
        _bottomLocked = bottom;
        _rightLocked = right;
        _leftLocked = left;
    }


    public void move(double x, double y){
        super.move(x,y);

        if(!_animator.isPlaying("walk")){
            _animator.setAnimation("walk");
        }
    }

    @Override
    public void update(Graphics2D g){
//        _animator.show(g);
        super.update(g);
        _handleMovement();
        handleAttack();
    }




    public Transform getTransform(){
        return _animator;
    }

    public void attack(){
        if(_rdyToAttack){
            _animator.setAnimation("attk");
            _attack = true;
            _rdyToAttack = false;
            _hitDelayTimer = _hitDelay;
        }
    }

    @Override
    void idle() {

    }
    @Override
    void handleAttack(){
        if(_attack){
            _hitDelayTimer -= Config.TIMER_DIF;
            if(_hitDelayTimer<=_hitBoxDelay){
//                System.out.println("setHitting(true)");
                setHitting(true);
            }
            if(_hitDelayTimer<=0){
                _rdyToAttack = true;
                _attack = false;
                _hitDelayTimer = _hitDelay;
                _animator.setAnimation("idle");
            }
        }
    }

    private void _handleMovement(){
        _keyPressed = false;
        if(_input.isKeyDown(KeyEvent.VK_UP) && getZ() > _topLocked){
            _keyPressed = true;
            move(0, -_yspeed);
        }
        if(_input.isKeyDown(KeyEvent.VK_DOWN) && getZ() < _bottomLocked){
            _keyPressed = true;
            move(0, _yspeed);
        }
        if(_input.isKeyDown(KeyEvent.VK_RIGHT) && _animator.getX() < _rightLocked) {
            _keyPressed = true;
            move(_xspeed,0);
        }
        if(_input.isKeyDown(KeyEvent.VK_LEFT) && _animator.getX() > _leftLocked) {
            _keyPressed = true;
            move(-_xspeed,0);
        }

        if(_input.isKeyDown(Config.PLAYER1_MELEE) && _attkKeyUp) {
            attack();
            _attkKeyUp = false;
            _keyPressed = true;
        }

        if(_input.isKeyUp(Config.PLAYER1_MELEE) && !_attkKeyUp){
            _attkKeyUp = true;
        }

        if(!_keyPressed && !_animator.isPlaying("idle") && !_animator.isPlaying("attk")){
            _animator.setAnimation("idle");
            _animator.translate(0,0);
        }
    }
}
